﻿using System;
using System.Collections.Generic;
using SilverChart.Core.Configuration;

namespace SilverChart.UnitTest.Core.Configuration {
    public class MockedConfigurationParser : AbstractConfigurationParser {
        public const String DOUBLE_VALUE_ITEM_NAME = "DoubleValue";
        public const String DOUBLE_VALUE = "0.01";

        public const String ERR_DOUBLE_VALUE_ITEM_NAME = "ERR-DoubleValue";
        public const String ERR_DOUBLE_VALUE = "ERROR";

        public const String INT32_VALUE_ITEM_NAME = "Int32Value";
        public const String INT32_VALUE = "23";
        public const Int32 INT32_REAL_VALUE = 23;

        public const String ERR_INT32_VALUE_ITEM_NAME = "ERR-Int32Value";
        public const String ERR_INT32_VALUE = "ERROR";

        public const String ALT_DOUBLE_VALUE_ITEM_NAME = "ALT-DoubleValue";
        public const String ALT_DOUBLE_VALUE = "This is not a Double";
        public const Double ALT_REAL_DOUBLE_VALUE = 0.5;

        public const String BOOLEAN_VALUE_ITEM_NAME = "BooleanValue_true";
        public const String BOOLEAN_VALUE = "true";
        public const Boolean BOOLEAN_REAL_VALUE = true;

        public const String BOOLEAN_VALUE_1_ITEM_NAME = "BooleanValue_1";
        public const String BOOLEAN_VALUE_1 = "1";
        public const Boolean BOOLEAN_REAL_VALUE_1 = true;

        public const String BOOLEAN_VALUE_0_ITEM_NAME = "BooleanValue_0";
        public const String BOOLEAN_VALUE_0 = "0";
        public const Boolean BOOLEAN_REAL_VALUE_0 = false;

        public const String BOOLEAN_VALUE_Y_ITEM_NAME = "BooleanValue_Y";
        public const String BOOLEAN_VALUE_Y = "Y";
        public const Boolean BOOLEAN_REAL_VALUE_Y = true;

        public const String BOOLEAN_VALUE_N_ITEM_NAME = "BooleanValue_N";
        public const String BOOLEAN_VALUE_N = "N";
        public const Boolean BOOLEAN_REAL_VALUE_N = false;

        public const String BOOLEAN_VALUE_YES_ITEM_NAME = "BooleanValue_Yes";
        public const String BOOLEAN_VALUE_YES = "Yes";
        public const Boolean BOOLEAN_REAL_VALUE_YES = true;

        public const String BOOLEAN_VALUE_NO_ITEM_NAME = "BooleanValue_No";
        public const String BOOLEAN_VALUE_NO = "no";
        public const Boolean BOOLEAN_REAL_VALUE_NO = false;

        public const String BOOLEAN_ERR_VALUE_ITEM_NAME = "ERR-BooleanValue";
        public const String BOOLEAN_ERR_VALUE = "ERROR";

        public const String STRING_VALUE_ITEM_NAME = "StringValue";
        public const String STRING_VALUE = "aString";

        public const String INT32_ARRAY_ITEM_NAME = "Int32[]";
        public const String INT32_ARRAY_VALUE_1 = "1";
        public const String INT32_ARRAY_VALUE_2 = "2";
        public const String INT32_ARRAY_VALUE_3 = "3";
        public const String INT32_ARRAY_VALUE_4 = "4";

        public const String ITEM_ARRAY_ITEM_NAME = "Item[]";
        public const String ITEM_ARRAY_ITEM_WITHOUT_ITEMTYPE_NAME = "Item-NonTyped[]";

        public const String NOT_EXISTED_ITEM_NAME = "Not_Existed_Item_Name";

        public const String UNKNOW_VALUE = "UNKNOWN";

        protected override ConfigurationItem CreateConfigurationItem(Type type, string itemName) {
            ConfigurationItem item = null;
            if (itemName != INT32_ARRAY_ITEM_NAME 
                && itemName != ITEM_ARRAY_ITEM_NAME 
                && itemName != ITEM_ARRAY_ITEM_WITHOUT_ITEMTYPE_NAME
                && itemName != NOT_EXISTED_ITEM_NAME) {
                MockedConfigurationParser parser = new MockedConfigurationParser();
                item = AbstractConfigurationItem.CreateInstance(type, parser);
                parser.Configuration = item;
            }
            return item;
        }

        public override string GetItemValue(string itemName) {
            if (itemName == DOUBLE_VALUE_ITEM_NAME) {
                return DOUBLE_VALUE;
            }
            else if (itemName == ALT_DOUBLE_VALUE_ITEM_NAME) {
                return ALT_DOUBLE_VALUE;
            }
            else if (itemName == ERR_DOUBLE_VALUE_ITEM_NAME) {
                return ERR_DOUBLE_VALUE;
            }
            else if (itemName == INT32_VALUE_ITEM_NAME) {
                return INT32_VALUE;
            }
            else if (itemName == ERR_INT32_VALUE_ITEM_NAME) {
                return ERR_INT32_VALUE;
            }
            else if (itemName == BOOLEAN_VALUE_ITEM_NAME) {
                return BOOLEAN_VALUE;
            }
            else if (itemName == BOOLEAN_VALUE_1_ITEM_NAME) {
                return BOOLEAN_VALUE_1;
            }
            else if (itemName == BOOLEAN_VALUE_0_ITEM_NAME) {
                return BOOLEAN_VALUE_0;
            }
            else if (itemName == BOOLEAN_VALUE_Y_ITEM_NAME) {
                return BOOLEAN_VALUE_Y;
            }
            else if (itemName == BOOLEAN_VALUE_N_ITEM_NAME) {
                return BOOLEAN_VALUE_N;
            }
            else if (itemName == BOOLEAN_VALUE_YES_ITEM_NAME) {
                return BOOLEAN_VALUE_YES;
            }
            else if (itemName == BOOLEAN_VALUE_NO_ITEM_NAME) {
                return BOOLEAN_VALUE_NO;
            }
            else if (itemName == STRING_VALUE_ITEM_NAME) {
                return STRING_VALUE;
            }

            return UNKNOW_VALUE;
        }

        protected override IList<ConfigurationItem> CreateConfigurationCollectionItem(Type type, string itemName) {
            if (itemName == ITEM_ARRAY_ITEM_NAME || itemName == ITEM_ARRAY_ITEM_WITHOUT_ITEMTYPE_NAME) {
                IList<ConfigurationItem> items = new List<ConfigurationItem>();
                for (int i = 0; i < 4; i++) {
                    MockedConfigurationParser parser = new MockedConfigurationParser();
                    ConfigurationItem item = AbstractConfigurationItem.CreateInstance(type, parser);
                    parser.Configuration = item;
                    items.Add(item);
                }
                return items;
            }
            return null;
        }

        protected override bool IsItemSupportCollection(string itemName) {
            return itemName == INT32_ARRAY_ITEM_NAME 
                || itemName == ITEM_ARRAY_ITEM_NAME 
                || itemName == ITEM_ARRAY_ITEM_WITHOUT_ITEMTYPE_NAME;
        }

        protected override IList<string> GetItemValues(string itemName) {
            IList<String> values = new List<String>();
            if (itemName == INT32_ARRAY_ITEM_NAME) {
                values.Add(INT32_ARRAY_VALUE_1);
                values.Add(INT32_ARRAY_VALUE_2);
                values.Add(INT32_ARRAY_VALUE_3);
                values.Add(INT32_ARRAY_VALUE_4);
            }
            return values;
        }
    }
}
